<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Admin\Controller;
use Think\Controller;

class ZchouController extends BaseController {
	/*
	 * 显示众筹项目
	 */
	public function index(){
		$info = M('info','pro_');
		$result = $info->select();
		$this->assign('result',$result);
		$this->display();
	}
	
	/*
	 * 发起众筹
	 */
	public function pubzchou(){
		
		$this->assign('jjhlist',$this->getjjhlist());
		$this->display();
	}
	
	/*
	 * 众筹记录
	 */
	public function record(){
	
		$this->display();
	}
	
	/*
	 * 添加、编辑众筹项目字段规则
	 */
	protected function validates(){
		$rules = array(
			array('pro_name','require','众筹名称不能为空！'),
			array('pro_img','require','缩略图不能为空！'),
			array('pro_starttime','require','开始时间不能为空！'),
			array('pro_endtime','require','结束时间不能为空！'),
			array('pro_summoney','require','众筹总金额不能为空！'),
			array('pro_content','require','众筹详情不能为空！'),
		);
		return $rules;
	}
	
	/*
	 * 添加众筹
	 */
	public function add(){
		$info = M('info','pro_');
		if(!$info->validate($this->validates())->create()){
			$this->error($info->getError(),'',1);
		}else{
			$info->pro_content = html_entity_decode(I('post.pro_content'));
			$info->pro_video = html_entity_decode(I('post.pro_video'));
			$result = $info->add();
			if($result !== false){
				//A(C('TABLE_LOG'))->index(1,'添加文档成功');
				$this->success('发布成功！','index');
			}else{
				//A(C('TABLE_LOG'))->index(0,'添加文档失败');
				$this->error('发布失败！','',1);
			}
		}
	}
	
	/*
	 * 编辑显示众筹
	 */
	public function editzchou($id=null){
		if(!empty($id)){
			$info = M('info','pro_');
			$result = $info->find($id);

			$this->assign('datecha',strtotime(date('Y-m-d'))-strtotime($result['pro_endtime']));
			$this->assign('info',$result);
			$this->assign('jjhlist',$this->getjjhlist());
			$this->display();
		}	
	}
	
	/*
	 * 编辑并保持众筹
	 */
	public function save($id=null){
		if(!empty($id)){
			$info = M('info','pro_');

			if(!$info->validate($this->validates())->create()){
				$this->error($info->getError(),'',1);
			}else{
				$info->pro_content = html_entity_decode(I('post.pro_content'));
				$info->pro_video = html_entity_decode(I('post.pro_video'));
				$result = $info->where(array('id'=>$id))->save();
				if($result !== false){
					$this->success('编辑成功！','/Admin/Zchou/index');
				}else{
					$this->error('编辑失败！','',1);
				}
			}
		}
	
	}
	
	/*
	 * 改变状态
	 */
	public function changestate($id=null,$state=null){
		$id = htmlspecialchars($id);
		$state = htmlspecialchars($state);
		if(!isset($state)) $this->error('非法参数！','',1);
		$state_result = $state?'0':'1';
		if(!empty($id)){
			$type = M('info','pro_');
			$type->pro_state = $state_result;
			$reult = $type->where(array('id'=>$id))->save();
			if($reult !== false){
				//$this->success('操作成功，跳转......');
				echo 1; //操作成功
			}else{
				//$this->error('操作失败！','',1);
				echo 2; //操作失败
			}
		}else{
			//$this->error('缺少参数，删除失败！','',1);
			echo 0; //非法参数
		}
	}
	
	/*
	 * 删除众筹项目
	 */
	public function delete($id=null){
		if(!empty($id)){
			$info = M('info','pro_');
			$result = $info->delete($id);
			if($result){
				$this->success('删除成功！','/Admin/Zchou/index');
			}else{
				$this->error('删除失败！','',1);
			}	
		}
	}
	
	/*
	 * 添加基金会信息
	 */
	public function jjhadd(){
		$rules = array(
			array('j_name','require','名称不能为空！'),
		);
		$d = D(C("TABLE_JIJINHUI"));
		if(!$d->validate($rules)->create()){
			$this->error($d->getError(),'',1);
		}else{
			$d->j_name = I('post.j_name');
			$d->j_content = html_entity_decode(I('post.content'));
			$result = $d->add();
			if($result !== false){
				$this->success('添加成功！','jijinhuilist');
			}else{
				$this->error('添加失败！','',1);
			}
		}
	}
	
	/*
	 * 基金会列表
	 */
	public function jijinhuilist(){
		$this->assign('jijinhuislist',$this->getjjhlist());
		$this->display();		
	}
	
	protected function getjjhlist(){
		$d = D(C("TABLE_JIJINHUI"));
		$res = $d->select();
		return $res;
	}
	
	public function jijinhuiedit(){
		$id = I('get.id',0);
		$d = D(C("TABLE_JIJINHUI"));
		$res = $d->find($id);
		$this->assign('article',$res);
		$this->display();
	}
	
	/*
	 * 编辑基金会
	 */
	public function jjhedit(){
		$id = I('get.id',0);
		$rules = array(
			array('j_name','require','名称不能为空！'),
		);
		$d = D(C("TABLE_JIJINHUI"));
		if(!$d->validate($rules)->create()){
			$this->error($d->getError(),'',1);
		}else{
			$d->j_name = I('post.j_name');
			$d->j_content = html_entity_decode(I('post.content'));
			$result = $d->where(array('id'=>$id))->save();
			if($result !== false){
				$this->success('编辑成功！','/Admin/Zchou/jijinhuilist');
			}else{
				$this->error('编辑失败！','',1);
			}
		}
	}
	
	/*
	 * 删除基金会
	 */
	public function jijinhuidelete(){
		$id = I('get.id',0);
		$d = D(C("TABLE_JIJINHUI"));
		$res = $d->delete($id);
		if($res !== false){
			$this->success('删除成功！','/Admin/Zchou/jijinhuilist');
		}else{
			$this->error('删除失败！','',1);
		}
	}
	
	


}